﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BO.News;
using ChannelVN.Cached.Common;
using GUI.Extensions;
using BO.Infrastructure.Utils;

namespace GUI.Services.Vote
{
    /// <summary>
    /// Summary description for VoteHandler
    /// </summary>
    public class VoteHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string command = "";
            if (context.Request.RequestType == "POST")
            {
                if (context.Request["command"] != null)
                {
                    command = context.Request["command"];
                }
                else
                {
                    return;
                }
            }
            else
            {
                if (context.Request.QueryString["command"] != null)
                {
                    command = context.Request.QueryString["command"];
                }
                else
                {
                    return;
                }
            }

            switch (command.ToLower())
            {
                case "viewresult":
                    ViewResult(context);
                    break;
                case "vote":
                    Vote(context);
                    break;
            }
        }

        private void ViewResult(HttpContext context)
        {
            var ajaxResponse = new AjaxResponse();

            try
            {
                var voteID = RequestExtensions.VoteId;
                if (voteID > 0)
                {
                    var vote = NewsBO.GetVote(voteID);

                    if (vote != null)
                    {
                        ajaxResponse.Data = vote;
                        ajaxResponse.Success = true;
                    }
                }
            }
            catch (Exception ex)
            {

                ajaxResponse.Data = ex.Message;
            }
            context.Response.Write(ajaxResponse.ToJson());
        }

        private void Vote(HttpContext context)
        {

            var voteID = RequestExtensions.VoteId;
            var ajaxResponse = new AjaxResponse();
            if (voteID > 0)
            {
                if (CheckVote(voteID))
                {
                    var qsVoteItemIDs = RequestExtensions.VoteItemIDs.Trim(',');
                    if (!string.IsNullOrEmpty(qsVoteItemIDs))
                    {
                        var arrVoteItemIDs = qsVoteItemIDs.Trim(',').Split(',');
                        var voteItemIDs = new int[arrVoteItemIDs.Length];
                        for (int i = 0; i < arrVoteItemIDs.Length; i++)
                        {
                            voteItemIDs[i] = PresentationUtils.Object2Integer(arrVoteItemIDs[i]);

                        }
                        ajaxResponse.Success = NewsBO.Vote(voteID, voteItemIDs);

                    }
                }
            }
            context.Response.Write(ajaxResponse.ToJson());
        }

        private bool CheckVote(int Vote_ID)
        {
            string cookieName = "PopupVote_" + Vote_ID;
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];

            if (cookie == null)
            {
                try
                {
                    cookie = new HttpCookie(cookieName);
                    cookie.Values["ID"] = Vote_ID.ToString();
                    cookie.Expires = DateTime.Now.AddMinutes(3);
                    HttpContext.Current.Response.Cookies.Add(cookie);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            return false;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}